package utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.*;

/**
 * 压缩工具
 */
public class ZipUtil {

    /**
     * 压缩
     *
     * @param src
     * @param out
     * @throws IOException
     */
    public static void compress(final String src, final OutputStream out) throws IOException {
        if (src == null) {
            return;
        }
        compress(src.getBytes(), out);
    }

    /**
     * 压缩
     *
     * @param src 源
     * @param out 输出流
     * @throws IOException
     */
    public static void compress(final byte[] src, final OutputStream out) throws IOException {
        if (src == null || src.length == 0 || out == null) {
            return;
        }
        ZipOutputStream zout = new ZipOutputStream(out);
        try {
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(src);
            zout.closeEntry();
        } finally {
            zout.close();
        }
    }

    /**
     * 压缩
     *
     * @param src 字符串
     * @return 二进制
     * @throws IOException
     */
    public static byte[] compress(final String src) throws IOException {
        if (src == null) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            compress(src, out);
            return out.toByteArray();
        } finally {
            out.close();
        }
    }

    /**
     * 压缩
     *
     * @param src
     * @return
     * @throws IOException
     */
    public static byte[] compress(final byte[] src) throws IOException {
        if (src == null) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            compress(src, out);
            return out.toByteArray();
        } finally {
            out.close();
        }
    }

    /**
     * 解压缩
     *
     * @param buf
     * @return
     * @throws IOException
     */
    public static String decompress(final byte[] buf) throws IOException {
        if (buf == null || buf.length == 0) {
            return null;
        }
        /**
         * 冒泡排序
         */



        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(buf));
        try {
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            return out.toString();
        } finally {
            zin.close();
            out.close();
        }
    }


    public static void main(String[] args) {

        String str="asdfasdfsadgasdgasgasfjasljglaksjglaskjgoqhingoqihbqoeibhqoeqeig我大使馆撒旦asfasg123124rkfajslgqltjiou12o啊可是觉得佛啊阿萨佛尔u荣SJDF啊速度佛3恩付款了SDN付款前fasdfasdfasdf;";
        try {
            byte[] compress = compress(str);
            System.out.println(compress.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
